﻿using System;
using System.ComponentModel;
using System.Web.UI;

namespace Altairis.Web.UI.WebControls {

    [TargetControlType(typeof(IEditableTextControl))]
    public class CookieStoreExtender : Control {
        private const int DEFAULT_COOKIE_EXPIRATION_DAYS = 30;
        private const string DEFAULT_COOKIE_NAME = "CSEXT";

        public CookieStoreExtender() {
            this.CookieExpirationDays = DEFAULT_COOKIE_EXPIRATION_DAYS;
            this.CookieName = DEFAULT_COOKIE_NAME;
        }

        #region Configuration properties

        [Category("Cookie"), DefaultValue(DEFAULT_COOKIE_EXPIRATION_DAYS)]
        [Description("Days to cookie expire. Defaults to one month.")]
        public int CookieExpirationDays {
            get { return (int)this.ViewState["CookieExpirationDays"]; }
            set { this.ViewState["CookieExpirationDays"] = value; }
        }

        [Category("Cookie"), DefaultValue(DEFAULT_COOKIE_NAME)]
        [Description("Storage cookie name. Defaults to 'CSEXT'.")]
        public string CookieName {
            get { return (string)this.ViewState["CookieName"]; }
            set { this.ViewState["CookieName"] = value; }
        }

        [Category("Cookie")]
        [Description("Cookie value name. Defaults to ID of target control.")]
        public string CookieValueName {
            get { return (string)this.ViewState["CookieValueName"]; }
            set { this.ViewState["CookieValueName"] = value; }
        }

        [Category("Behavior"), Description("ID of target IEditableTextControl.")]
        [IDReferenceProperty(typeof(IEditableTextControl))]
        public string TargetControlID {
            get { return (string)this.ViewState["TargetControlID"]; }
            set { this.ViewState["TargetControlID"] = value; }
        }

        private string EffectiveCookieValueName {
            get {
                if (string.IsNullOrEmpty(this.CookieValueName)) return this.TargetControlID;
                return this.CookieValueName;
            }
        }

        #endregion Configuration properties

        protected override void OnLoad(EventArgs e) {
            base.OnLoad(e);

            // Get target TextBox
            var targetTextControl = this.NamingContainer.FindControl(this.TargetControlID) as IEditableTextControl;
            if (targetTextControl == null) throw new ArgumentException("Target control does not exist or does not implement IEditableTextControl interface.");

            // Attach handler to store changed value
            targetTextControl.TextChanged += new EventHandler(TargetTextControl_TextChanged);

            // Load extended control value from cookie
            if (!this.Page.IsPostBack) targetTextControl.Text = Cookies.Get(this.CookieName, this.EffectiveCookieValueName);
        }

        private void TargetTextControl_TextChanged(object sender, EventArgs e) {
            // Get target TextBox
            var targetTextControl = sender as IEditableTextControl;

            // Set or create cookie
            Cookies.Set(this.CookieName, this.EffectiveCookieValueName, targetTextControl.Text, this.CookieExpirationDays);
        }
    }
}